// pages/home/home.js
const globalObj = getApp();
let QQMapWX = require('../../utils/qqmap-wx-jssdk');
let qqmapsdk;
import {
  theaters
} from '../../utils/home/hotMovie/index';
import {
  comming
} from '../../utils/home/commingMovie/index';
import {
  selectPreview
} from '../../utils/home/selectPreview/index';
import { subject } from '../../utils/home/subject/index';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aboveDistance: globalObj.globalData.aboveDistance,
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 500,
    bannerList: ['/assets/images/home/banner_1.jpg',
      '/assets/images/home/banner_2.jpg', '/assets/images/home/banner_3.jpg', '/assets/images/home/banner_4.jpg'
    ],
    city: '',
    currentIndex: 0,
    imgActiveImg: 'active',
    theatersMovie: [],
    scollFlag: true,
    commingList: [],
    top250: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: '2ZDBZ-BO2KO-G5GW5-SONYC-WU2OH-PJB5P'
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bannerChange(e) {
    // console.log(e);
    this.setData({
      currentIndex: e.detail.current
    })
  },
  getUserLocation() {
    const _this = this;
    wx.getLocation({
      success(res) {
        // console.log(res);
        qqmapsdk.reverseGeocoder({
          location: `${res.latitude},${res.longitude}`,
          success(val) {
            console.log('位置信息',val);
            wx.setStorageSync('address', val.result.address);
            // console.log(val.result.address_component.city.replace(/市|县/, ""));
            let add = val.result.address_component.city.replace(/市|县/, "");
            wx.setStorageSync('city', add);
            wx.setStorageSync('location', val.result.ad_info.location);
            _this.setData({
              city: add
            })
          }
        })
      }
    })
  },
  async getTheatersMovie() {
    let data = await theaters();
    console.log(data)
    this.setData({
      theatersMovie: data.data.data.list
    })
  },
  async getCommingMovie() {
    let data = await comming();
    // console.log(data)
    // let d = data.data.data.list[0];
    // let date = new Date(d.update_time);
    // let m = date.getMonth() + 1;
    // let day = date.getDay();
    // // console.log(this.forMateTime(m),this.forMateTime(day))
    // let time = this.forMateTime(m) + '月' + this.forMateTime(day) + '日';
    data.data.data.list.forEach(el => {
      let date = new Date(el.update_time);
      let m = date.getMonth() + 1;
      let day = date.getDay();
      // console.log(this.forMateTime(m),this.forMateTime(day))
      let time = this.forMateTime(m) + '月' + this.forMateTime(day) + '日';
      el.sj = time;
    });
    this.setData({
      commingList: data.data.data.list
    })
  },
  async getSelectPreview() {
    const _this = this;
    let data = await selectPreview();
    console.log('电影的数据',data);
    // subject({ mId:  })
    let list = data.data.data;
    list.forEach( el => {
      subject({ mId: el.mId }).then(res => {
        // console.log('电影介绍',res)
        el.summary = res.data.data.summary.trim();
        _this.setData({
          top250: list
        })
      })
    })
    // "27119724"
    // setTimeout(() => {
      // this.setData({
      //   top250: list
      // })
    // }, 0)
   
    console.log('top250', this.data.top250)
  },
  imgError(val) {
    console.log('图片错误', val)
  },
  forMateTime(val) {
    return val < 10 ? '0' + val : val;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserLocation();
    this.getTheatersMovie();
    this.getCommingMovie();
    this.getSelectPreview();
    console.log(globalObj.globalData)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})